﻿using db_manage.dll.common;
using db_manage.dll.manage;
using db_manage.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace db_manage.page
{
    public partial class form_diff : Form
    {
        private static form_db_info form_db_info { get => manage.from_get<form_db_info>(); }
        public form_diff()
        {
            InitializeComponent();
            new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    Thread.Sleep(2000);
                    Action action = () =>
                    {
                        data_update();
                    };
                    if (this.IsDisposed)
                    {
                        break;
                    }
                    this?.Invoke(action);
                }
            })).Start();
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        public void data_update(Boolean isNew = false)
        {
            var now_list = manage.db_diff_info_dic?.Values?.SelectMany(x => x) ?? new List<db_diff_info>();
            l_total.Text = "总条数：" + now_list.Count();
            l_success.Text = "成功条数：" + now_list.Where(x => x.status == "修复成功").Count();

            if (!this.cb_showdata.Checked || !(now_list?.Any() ?? false))
            {
                this.dgvfixdata.DataSource = null;
                return;
            }
            if (isNew ||
                this.dgvfixdata.DataSource == null ||
                now_list.Count() != ((List<db_diff_info>)this.dgvfixdata.DataSource).Count())
            {
                this.dgvfixdata.DataSource = new List<db_diff_info>(now_list);
            }
            else
            {
                this.dgvfixdata.Refresh();
            }
        }

        /// <summary>
        /// 锁定所有按钮
        /// </summary>
        private void button_lock()
        {
            this.bt_diffcheck.Enabled = false;
            this.bt_fix.Enabled = false;
            this.bt_checkandfix.Enabled = false;
        }

        /// <summary>
        /// 解锁所有按钮
        /// </summary>
        private void button_unlock()
        {
            this.BeginInvoke(new Action(() =>
            {
                this.bt_diffcheck.Enabled = true;
                this.bt_fix.Enabled = true;
                this.bt_checkandfix.Enabled = true;
            }));
        }

        /// <summary>
        /// 比较差异
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_diffcheck_Click(object sender, EventArgs e)
        {
            var templatedbname = tb_templatedbname.Text;
            if (string.IsNullOrEmpty(templatedbname))
            {
                MessageBox.Show("未填写模板数据库名称");
                return;
            }
            form_db_info.ShowAndBringToFront();
            button_lock();
            MessageBox.Show(manage.db_diff_check(templatedbname, button_unlock));
        }

        /// <summary>
        /// 修复差异
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_fix_Click(object sender, EventArgs e)
        {
            button_lock();
            MessageBox.Show(manage.db_diff_fix(button_unlock));
        }

        /// <summary>
        /// 比对数据库并修复差异
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_checkandfix_Click(object sender, EventArgs e)
        {
            bt_diffcheck_Click(null, null);
            bt_fix_Click(null, null);
        }
    }
}
